package com.dqv5.k8s.userinfo.controller;

import com.dqv5.k8s.userinfo.entity.UserInfo;
import com.dqv5.k8s.userinfo.service.UserService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author duqian
 * @date 2020/9/25
 */
@RestController
public class UserController {
    @Resource
    private UserService userService;

    @GetMapping("/queryAll")
    public ResponseEntity<Object> queryAll() {
        List<UserInfo> list = userService.queryAll();
        Map<String, Object> result = new HashMap<>();
        result.put("message", "操作成功");
        result.put("success", true);
        result.put("data", list);
        return ResponseEntity.ok(result);
    }

    @GetMapping("/detail/{id}")
    public ResponseEntity<Object> detail(@PathVariable Long id) {
        UserInfo userInfo = userService.queryById(id);
        Map<String, Object> result = new HashMap<>();
        result.put("message", "操作成功");
        result.put("success", true);
        result.put("data", userInfo);
        return ResponseEntity.ok(result);
    }

    @PostMapping("/insert")
    public ResponseEntity<Object> insert(@RequestBody UserInfo userInfo) {
        userService.insert(userInfo);
        Map<String, Object> result = new HashMap<>();
        result.put("message", "操作成功");
        result.put("success", true);
        result.put("data", null);
        return ResponseEntity.ok(result);
    }

    @PostMapping("/update")
    public ResponseEntity<Object> update(@RequestBody UserInfo userInfo) {
        userService.update(userInfo);
        Map<String, Object> result = new HashMap<>();
        result.put("message", "操作成功");
        result.put("success", true);
        result.put("data", null);
        return ResponseEntity.ok(result);
    }

    @PostMapping("/delete/{id}")
    public ResponseEntity<Object> delete(@PathVariable Long id) {
        userService.delete(id);
        Map<String, Object> result = new HashMap<>();
        result.put("message", "操作成功");
        result.put("success", true);
        result.put("data", null);
        return ResponseEntity.ok(result);
    }


}
